package cn.pugle.redis.pubsub;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import static cn.pugle.redis.pubsub.Publisher.HOST;
import static cn.pugle.redis.pubsub.Publisher.PORT;

/**
 * @author tzp
 * @date 2017/10/17
 */
public class Subscriber extends JedisPubSub {
    private static Jedis jedis = new Jedis(HOST, PORT);
    private static Subscriber subscriber = new Subscriber();


    public static void main(String[] args) {
        System.out.println("start");
        jedis.subscribe(subscriber, "c1", "foo");
        //这里是阻塞的, 交给用户处理线程相关问题了
        System.out.println("end");
        jedis.close();
    }

    @Override
    public void onMessage(String channel, String message) {
        System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message));
//        subscriber.unsubscribe();
    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",
                channel, subscribedChannels));
    }

    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",
                channel, subscribedChannels));

    }
}
